package org.ajax.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ajax.dao.GoodsDao;
import org.ajax.dao.impl.GoodsDaoImpl;
import org.ajax.entity.Good;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author CSY
 * @date 2024-11-20
 */
@WebServlet("/goods.do")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        GoodsDao dao = new GoodsDaoImpl();

        List<Good> list = dao.selectGood();

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        String json = gson.toJson(list);

        PrintWriter out = resp.getWriter();

        out.println(json);

        out.flush();
        out.close();
    }
}
